﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mesoBoard.Data.Repositories;
using mesoBoard.Data;
using System.Web.Mvc.Html;

namespace mesoBoard.Common.HtmlHelpers
{
    public static partial class mesoBoardHtmlHelpers
    {
        public const string SEPERATOR = " » ";

        public static string ForumLink(this HtmlHelper html, Forum TheForum, string LinkClass, bool BoldText)
        {
            return ForumLink(html, TheForum.ForumID, LinkClass, BoldText);
        }

        public static string ForumLink(this HtmlHelper html, int ForumID, string LinkClass, bool BoldText)
        {
            ForumRepository ForumRep = new ForumRepository(new mbEntities());
            Forum TheForum = ForumRep.GetForum(ForumID);
            LinkClass = LinkClass ?? "forum_link";
            UrlHelper Url = new UrlHelper(html.ViewContext.RequestContext);
            return string.Format("<a href='{0}' {1} >{2}</a>",
               Url.Action("ViewForum", "Board", new { ForumID = TheForum.ForumID }),
               BoldText ? "style='font-weight: bold'" : "",
               TheForum.Name);
        }

        public static string BoardIndexLink(this HtmlHelper html, string Text, string LinkClass, bool BoldText)
        {
            UrlHelper Url = new UrlHelper(html.ViewContext.RequestContext);
            ConfigRepository ConfigRep = new ConfigRepository(new mbEntities());
            return string.Format("<a href='{0}' class='{1}{2}'>{3}</a>",
                Url.Action("Index", "Board"),
                LinkClass,
                BoldText ? " bold" : "",
                Text);

        }

        public static string UserProfileLink(this HtmlHelper html, string UserNameOrID)
        {
            UrlHelper Url = new UrlHelper(html.ViewContext.RequestContext);
            TagBuilder link = new TagBuilder("a");
            link.Attributes.Add("href", Url.Action("UserProfile", "Members", new { UserNameOrID = UserNameOrID }));
            link.Attributes.Add("title", "User Profile");
            link.InnerHtml = UserNameOrID;
            return link.ToString();
        }

        public static string ThreadLink(this HtmlHelper html, Thread TheThread, string LinkClass, bool BoldText, bool LastPost)
        {
            LinkClass = LinkClass ?? "thread_link";
            UrlHelper Url = new UrlHelper(html.ViewContext.RequestContext);
            object routeing;
            if (LastPost)
                routeing = new { ThreadID = TheThread.ThreadID, Page = 1, LastPost = LastPost };
            else
                routeing = new { ThreadID = TheThread.ThreadID, Page = 1 };

            return string.Format("<a href='{0}' class='{1}{2}'>{3}</a>",
                Url.Action("ViewThread", "Board", routeing),
                LinkClass,
                BoldText ? "bold" : "",
                html.Encode(TheThread.FirstPost.Title));
        }


        public static string ThreadLink(this HtmlHelper html, int ThreadID, string LinkClass, bool BoldText, bool LastPost)
        {
            ThreadRepository ThreadRep = new ThreadRepository(new mbEntities());
            Thread TheThread = ThreadRep.GetThread(ThreadID);
            return html.ThreadLink(TheThread, LinkClass, BoldText, LastPost);
        }

        public static string ImageLink(this HtmlHelper html, string Text, string Link,
            string ImageSource, string TitleAndAlt, string CssClass)
        {
            UrlHelper Url = new UrlHelper(html.ViewContext.RequestContext);
            Link = Url.Content(Link);
            if (CssClass == "image_link")
            {
                return string.Format("<a href='{0}' title='{2}' class='{4}'><img src='{1}' alt='{2}' title='{2}' />{3}</a>",
                    Link,
                    Url.Content(ImageSource),
                    TitleAndAlt,
                    Text,
                    CssClass);
            }
            else
            {
                return string.Format("<a href='{0}' title='{2}' class='{4}'><img src='{1}' alt='{2}' title='{2}' /></a><a href='{0}' title='{2}' class='{4}'>{3}</a>",
                    Link,
                    Url.Content(ImageSource),
                    TitleAndAlt,
                    Text,
                    CssClass);
            }
        }

        public static string BreadCrumbThreadLink(this HtmlHelper html, Thread TheThread)
        {
                return BoardIndexLink(html, "Board Index", "", false) + " » " +
                    ForumLink(html, TheThread.Forum, "", false) + " » " +
                    ThreadLink(html, TheThread, "", true, false);
        }

        public static string BreadCrumbThreadLink(this HtmlHelper html, int PostID, int ThreadID)
        {
            if (ThreadID == 0)
            {
                PostRepository PostRep = new PostRepository(new mbEntities());
                Post ThePost = PostRep.GetPost(PostID);
                return BoardIndexLink(html, "Board Index", "", false) + " » " +
                    ForumLink(html, ThePost.Thread.Forum, "", false) + " » " +
                    ThreadLink(html, ThePost.Thread, "", true, false);
            }
            else
            {
                ThreadRepository ThreadRep = new ThreadRepository(new mbEntities());
                Thread TheThread = ThreadRep.GetThread(ThreadID);
                return BoardIndexLink(html, "Board Index", "", false) + " » " +
                    ForumLink(html, TheThread.Forum, "", false) + " » " +
                    ThreadLink(html, TheThread, "", true, false);
            }
        }

        public static string BreadCrumbThreadLink(this HtmlHelper html, Thread thread, Forum forum)
        {
            return string.Concat(
                html.ActionLink("Board Index", "Index", "Board"), 
                SEPERATOR,
                html.ActionLink(forum.Name, "ViewForum", "Board", new { ForumID = forum.ForumID }, null),
                SEPERATOR,
                html.ActionLink(thread.Title, "ViewThread", "Board", new { ThreadID = thread.ThreadID }, new { style = "font-weight:bold" })
                );
        }

        public static string BreadCrumbForumLink(this HtmlHelper html, Forum forum)
        {
            return string.Concat(html.ActionLink("Board Index", "Index", "Board"), SEPERATOR,
                html.ActionLink(forum.Name, "ViewForum", "Board", new { ForumID = forum.ForumID }, new { style = "font-weight:bold" }));
        }

        public static string BreadCrumbForumLink(this HtmlHelper html, int ForumID)
        {
            ForumRepository ForumRep = new ForumRepository(new mbEntities());
            Forum TheForum = ForumRep.GetForum(ForumID);
            return BoardIndexLink(html, "Board Index", "", false) + " » " +
                ForumLink(html, TheForum, "", true);

        }
    }
}
